package com.taotao.search.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.SearchResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.search.service.ISolrService;

/**
 * 索引库维护
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/solr/manager")
public class SolrController {
	@Autowired
	private ISolrService solrService;

	/**
	 * 将数据库的数据导入索引库
	 * 
	 * @return
	 */
	@RequestMapping("/importAll")
	@ResponseBody
	public TaotaoResult importAllItemsToSolr() {
		TaotaoResult taotaoResult = this.solrService.importAllItemsToSolr();
		return taotaoResult;
	}

	/**
	 * 查询索引库
	 */
	@RequestMapping("/query")
	@ResponseBody
	public TaotaoResult search(@RequestParam(value = "q") String queryString,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "60") Integer rows) {
		if (StringUtils.isBlank(queryString)) {
			return TaotaoResult.build(400, "查询条件不能为空");
		}
		SearchResult search = null;
		try {
			queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
			search = this.solrService.search(queryString, page, rows);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return TaotaoResult.ok(search);
	}
}
